//引入css文件
require("../css/index.less")
require("../fonts/demo.css")
require("../lib/swiper/swiper-bundle.css")

document.ready(function () {
    setTimeout(function () {
        window.$utils.checkLogin()
    }, 500)

    // 配置轮播插件
    new Swiper('.swiper-container', {
        loop: true, // 循环模式选项
        autoplay: {
            disableOnInteraction: false,
        },// 操作swiper之后自动切换不会停止，每次都会重新启动autoplay
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
        }
    })
    window.$utils.creatFooter(0)


    //获取首页数据
    let uid = JSON.parse(window.localStorage.getItem("user"))
    //获取元素
    console.log(uid);
    let span = document.querySelector(".p1 span")
    let Num = document.querySelector(".badge p span")
    let r = document.querySelector(".p2")
    let punchBox = document.querySelector(".one .punch div")

    window.$http.get(`/headPageInfo?userId=${uid.userId}`, function (res) {//传入参数
        console.log(res);
        r.innerHTML = res.data.rank
        Num.innerHTML = res.data.insigniaNum
        span.innerHTML = res.data.punchIn
        if (res.data.isPunch == "true") {//根据打卡状态进行判断
            //页面刷新也要保证类名和内容已经更改
            punchBox.classList.add("punched")
            punchBox.innerHTML = "已打卡"
        } else { //打卡状态为false,则可进行打开,即给按钮注册点击事件
            //打卡盒子设置点击事件
            punchBox.addEventListener("click", punch)
        }
    })



    function punch() {
        window.$http.get("/clockIn?userId=" + uid.userId, function (res) {
            if (res.status == 0) { //请求发送成功才执行后面操作
                span.innerHTML = span.innerHTML - 0 + 1 //打卡成功,更改打卡天数,根据隐式转换
                punchBox.classList.add("punched") //添加类名
                punchBox.removeEventListener("click", punch) //点击打卡后,移除点击事件,避免重复点击
                punchBox.innerHTML = "已打卡" //修改内容为已打卡
            }
        })
    }
})
